package com.exartisan.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.exartisan.bean.Event;
import com.exartisan.bean.FbComment;
import com.exartisan.network.NetworkUtility;
import com.exartisan.roundedimageview.RoundedImageView;
import com.exartisan.util.FontUtil;
import com.exartisan.yn_event.R;

public class FeedAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Context ctx;
	private Event event;
	private ArrayList<FbComment> comments;
	private boolean liked = false;

	public FeedAdapter(Context ctx, Event event, ArrayList<FbComment> comments) {
		inflater = LayoutInflater.from(ctx);
		this.ctx = ctx;
		this.event = event;
		this.comments = comments;
	}

	@Override
	public int getCount() {
		return (comments.size() + 1);
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		if(pos == 0) {
			EventInfoViewHolder eInfoViewHolder;
			if (convertView==null||(convertView.findViewById(R.id.tv_event_desc) == null)){
				eInfoViewHolder = new EventInfoViewHolder();
			convertView = inflater.inflate(R.layout.event_basic_info_row, null);
			eInfoViewHolder.tvEventDesc = (TextView) convertView.findViewById(R.id.tv_event_desc);
			eInfoViewHolder.btnLike = (ImageButton) convertView.findViewById(R.id.btn_like);
			eInfoViewHolder.btnComment = (ImageButton) convertView.findViewById(R.id.btn_comment);
			//eInfoViewHolder.tvJoin = (TextView) convertView.findViewById(R.id.tv_join);
			//eInfoViewHolder.tvDetail = (TextView) convertView.findViewById(R.id.tv_detail);
			eInfoViewHolder.imageViewEventCover = (RoundedImageView) convertView.findViewById(R.id.img_event);
			eInfoViewHolder.tvEventDesc.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.TAHOMABD));
			//eInfoViewHolder.tvJoin.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.TAHOMA));
			//eInfoViewHolder.tvDetail.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.TAHOMA));
			convertView.setTag(eInfoViewHolder);
			} else eInfoViewHolder = (EventInfoViewHolder) convertView.getTag();
			eInfoViewHolder.tvEventDesc.setText(event.getEventName());
			eInfoViewHolder.imageViewEventCover.setImageUrl(event.getThumbUrl());
			if(liked) eInfoViewHolder.btnLike.setBackgroundResource(R.drawable.btn_liked);
			else eInfoViewHolder.btnLike.setBackgroundResource(R.drawable.btn_like);
		}
		else {
			FeedViewHolder fViewHolder;
			if (convertView==null||(convertView.findViewById(R.id.tv_event_desc)!=null)){
				fViewHolder = new FeedViewHolder();
			convertView = inflater.inflate(R.layout.feed_row, null);
			fViewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_user_name);
			fViewHolder.tvPostTime = (TextView) convertView.findViewById(R.id.tv_post_time);
			fViewHolder.imageViewUserPicture = (RoundedImageView) convertView.findViewById(R.id.img_user);
			fViewHolder.tvPostContent = (TextView) convertView.findViewById(R.id.tv_post_content);
			fViewHolder.tvUserName.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.TAHOMABD));
			fViewHolder.tvPostTime.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.ARIAL));
			fViewHolder.tvPostContent.setTypeface(FontUtil.loadFontFromAssets(ctx, FontUtil.ARIAL));
			convertView.setTag(fViewHolder);
			} else
				fViewHolder = (FeedViewHolder) convertView.getTag();
			fViewHolder.tvPostContent.setText(comments.get(pos - 1).getMessage());
			fViewHolder.tvPostTime.setText(comments.get(pos - 1).getCreated_time());
			fViewHolder.tvUserName.setText(comments.get(pos - 1).getFbUser().getName());
			fViewHolder.imageViewUserPicture.setImageUrl(NetworkUtility.GRAPH_API_URL + "/" + comments.get(pos - 1).getFbUser().getId() + "/picture");
		}
		return convertView;
		
	}
	
	public void setFbComments(ArrayList<FbComment> comments){
		this.comments = comments;
	}
	
	public void setLiked(boolean liked){
		this.liked = liked;
	}
	
	
	class EventInfoViewHolder{
		TextView tvEventDesc;
		TextView tvJoin;
		TextView tvDetail;
		RoundedImageView imageViewEventCover;
		ImageButton btnLike;
		ImageButton btnComment;
	}
	
	class FeedViewHolder{
		TextView tvUserName;
		TextView tvPostTime;
		TextView tvPostContent;
		RoundedImageView imageViewUserPicture;
		ImageButton btnLike;
		ImageButton btnComment;
	}

}
